/***************************************************************************
 *   Copyright (C) 2007 by Ziman & Xloverin                                *
 *   ziman@centrum.sk, xloverin@gmail.com                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "config.h"
#include <QtSql/QSqlQuery>
#include <QVariant>
#include <stdexcept>

void Config::set(QString key, const char *val)
{
	set(key, QString(val));
}

QString Config::get(QString key, const char *def)
{
	return get(key, QString(def));
}

QString Config::get(QString key, QString def)
{
	QSqlQuery query;
	query.prepare("SELECT value FROM config WHERE key = :key");
	query.bindValue(":key", key);
	query.setForwardOnly(true);
	query.exec();

	if (query.next()) {
		return query.value(0).toString();
	} else {
		return def;
	}
}

bool Config::get(QString key, bool def)
{
	QSqlQuery query;
	query.prepare("SELECT value FROM config WHERE key = :key");
	query.bindValue(":key", key);
	query.setForwardOnly(true);
	query.exec();

	if (query.next()) {
		return query.value(0).toBool();
	} else {
		return def;
	}
}

int Config::get(QString key, int def)
{
	QSqlQuery query;
	query.prepare("SELECT value FROM config WHERE key = :key");
	query.bindValue(":key", key);
	query.setForwardOnly(true);
	query.exec();

	if (query.next()) {
		return query.value(0).toInt();
	} else {
		return def;
	}
}

void Config::set(QString key, QString val)
{
	QSqlQuery query;
	query.prepare("INSERT OR REPLACE INTO config (key, value) VALUES (:key, :val)");
	query.bindValue(":key", key);
	query.bindValue(":val", val);
	query.exec();
}

void Config::set(QString key, bool val)
{
	QSqlQuery query;
	query.prepare("INSERT OR REPLACE INTO config (key, value) VALUES (:key, :val)");
	query.bindValue(":key", key);
	query.bindValue(":val", val);
	query.exec();
}

void Config::set(QString key, int val)
{
	QSqlQuery query;
	query.prepare("INSERT OR REPLACE INTO config (key, value) VALUES (:key, :val)");
	query.bindValue(":key", key);
	query.bindValue(":val", val);
	query.exec();
}


Config::Config(QSqlDatabase *db)
{
	m_db = db;
}


Config::~Config()
{
}


